//
//  PaymentScenicTableViewCell.swift
//  LeTu
//
//  Created by gy on 2024/1/10.
//

import UIKit

class PaymentScenicTableViewCell: BaseTableViewCell {

    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        configUI()
    }
    override func layoutSubviews() {
        super.layoutSubviews()
        
        DispatchQueue.main.async {
            self.imgView.layerCornerRadius(self.imgView.height/2)
        }
    }
    static func getCellHeight() -> CGFloat {
        return 14 + 60 + 14 + CGFloat.app_lineHieght + 64
    }
    func setScenicModel(_ scenicModel: ScenicModel) {
        
        // 设置景区名称
        nameLabel.text = LanguageManager.shared.currentLanguage == .en ? scenicModel.englishName:scenicModel.name
        
        let scenicLevelText = "\(scenicModel.scenicLevel)A"
        scenicLevelLabel.text = scenicLevelText
        scenicLevelLabel.snp.updateConstraints { make in
            make.width.equalTo(scenicLevelText.widthWithLimit(CGSize(width: CGFloat.greatestFiniteMagnitude, height: 14), font: kSetPingFangRegular(11))+10)
        }
        
        let audioCountText = "\(scenicModel.storyNum)"+"处讲解".localized
        audioCountLabel.attributed.text = "\(.image(UIImage(named: "play_white_small")!))\(audioCountText)"
        audioCountLabel.snp.updateConstraints { make in
            make.width.equalTo(audioCountLabel.sizeThatFits(CGSize(width: CGFloat.greatestFiniteMagnitude, height: 14)).width + 10)
        }
        
        listenCountLabel.text = "累计".localized+" \(scenicModel.listeners) "+"人已购买".localized
        
        // 设置景区图片
        imgView.sd_setImage(with: URL.encodedURL(string: scenicModel.picUrl))
    }
    
    // MARK: - configUI
    func configUI() {
        
        backgroundColor = .white
        
        contentView.addSubview(scenicBackView)
        scenicBackView.addSubview(imgView)
        scenicBackView.addSubview(nameLabel)
        scenicBackView.addSubview(scenicLevelLabel)
        scenicBackView.addSubview(audioCountLabel)
        contentView.addSubview(lineView)
        contentView.addSubview(listenCountLabel)
        
        scenicBackView.snp.makeConstraints { make in
            make.left.equalTo(22)
            make.right.equalTo(-22)
            make.top.equalTo(14)
            make.height.equalTo(60)
        }
        imgView.snp.makeConstraints { make in
            make.left.top.bottom.equalToSuperview()
            make.width.equalTo(imgView.snp.height)
        }
        nameLabel.snp.makeConstraints { make in
            make.left.equalTo(imgView.snp.right).offset(12)
            make.right.equalToSuperview()
            make.bottom.equalTo(imgView.snp.centerY)
        }
        scenicLevelLabel.snp.makeConstraints { make in
            make.left.equalTo(nameLabel)
            make.top.equalTo(imgView.snp.centerY).offset(4)
            make.width.equalTo(0)
            make.height.equalTo(14)
        }
        audioCountLabel.snp.makeConstraints { make in
            make.left.equalTo(scenicLevelLabel.snp.right).offset(6)
            make.height.equalTo(14)
            make.centerY.equalTo(scenicLevelLabel)
            make.width.equalTo(0)
        }
        lineView.snp.makeConstraints { make in
            make.left.equalTo(16)
            make.right.equalTo(-16)
            make.top.equalTo(scenicBackView.snp.bottom).offset(20)
            make.height.equalTo(CGFloat.app_lineHieght)
        }
        listenCountLabel.snp.makeConstraints { make in
            make.left.right.equalTo(scenicBackView)
            make.top.equalTo(lineView.snp.bottom).offset(32)
        }
        
    }
    private lazy var scenicBackView = UIView()
    private lazy var imgView: UIImageView = {
        let imgView = UIImageView()
        imgView.contentMode = .scaleAspectFill
        return imgView
    }()
    private var nameLabel: UILabel = {
        let nameLabel = UILabel()
        nameLabel.font = kSetPingFangMedium(20)
        return nameLabel
    }()
    private lazy var scenicLevelLabel: UILabel = {
        let scenicLevelLabel = UILabel()
        scenicLevelLabel.textColor = .app_white
        scenicLevelLabel.font = kSetPingFangRegular(11)
        scenicLevelLabel.textAlignment = .center
        scenicLevelLabel.backgroundColor = UIColor.hex(hexString: "#9C4615")
        scenicLevelLabel.layer.cornerRadius = 7
        scenicLevelLabel.layer.masksToBounds = true
        return scenicLevelLabel
    }()
    private lazy var audioCountLabel: UILabel = {
        let audioCountLabel = UILabel()
        audioCountLabel.textColor = .app_white
        audioCountLabel.font = kSetPingFangRegular(10)
        audioCountLabel.textAlignment = .center
        audioCountLabel.backgroundColor = UIColor.hex(hexString: "#E7BA1B")
        audioCountLabel.layer.cornerRadius = 7
        audioCountLabel.layer.masksToBounds = true
        return audioCountLabel
    }()
    private lazy var lineView: UIView = {
        let lineView = UIView()
        lineView.backgroundColor = .app_lineColor
        return lineView
    }()
    private var listenCountLabel: UILabel = {
        let listenCountLabel = UILabel()
        listenCountLabel.font = kSetPingFangRegular(12)
        listenCountLabel.textColor = UIColor.hex(hexString: "#A1A1A1")
        return listenCountLabel
    }()
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }

}
